package com.example.administrator.funchat.activity;


import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import com.example.administrator.funchat.Contans.Config;
import com.example.administrator.funchat.R;
import com.example.administrator.funchat.model.User;
import com.google.gson.Gson;


import org.xutils.common.Callback;
import org.xutils.http.HttpMethod;
import org.xutils.x;

public class SearchFriendActivity extends AppCompatActivity implements View.OnClickListener {


    private TextView btn_back;
    private EditText et_phone;
    private Button btn_search;
    private ProgressDialog dialog;
    private TextView tv_no_exist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_friend);
        initView();
    }

    private void initView() {
        btn_back = (TextView) findViewById(R.id.btn_back);
        et_phone = (EditText) findViewById(R.id.et_phone);
        btn_search = (Button) findViewById(R.id.btn_search);
        btn_back.setOnClickListener(this);
        btn_search.setOnClickListener(this);
        tv_no_exist = (TextView) findViewById(R.id.tv_no_exist);

    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_search:
                //搜索好友
                dialog = new ProgressDialog(this);
                dialog.setMessage("正在加载");
                dialog.setCancelable(true);
                dialog.show();
                org.xutils.http.RequestParams params = new org.xutils.http.RequestParams(Config.SEARCH_FRIEND);
                params.addBodyParameter("phone", et_phone.getText().toString());
                x.http().request(HttpMethod.POST, params, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String s) {
                        dialog.dismiss();
                        if (s.equals("error")) {//返回error，就不存在改好友用户
                            tv_no_exist.setVisibility(View.VISIBLE);
                        } else if (s == null) {
                            Toast.makeText(SearchFriendActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                        } else {//返回该搜索好友信息
                            Toast.makeText(x.app(), s, Toast.LENGTH_SHORT).show();
                            Log.i("add", "onSuccess: 好友信息==" + s);
                            Gson g = new Gson();
                            User u = g.fromJson(s, User.class);
                            Log.i("add", "onSuccess: phone==" + u.getPhone());
                            Intent intent = new Intent(SearchFriendActivity.this, FriendInformationActivity.class);
                            intent.putExtra("info", u);
                            startActivity(intent);

                        }
                    }

                    @Override
                    public void onError(Throwable ex, boolean b) {
                        Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onCancelled(CancelledException e) {
                        Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onFinished() {

                    }
                });
                break;

        }
    }
}
